package com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.senegal.R;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogItemClickListener;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogNumberInput;
import com.collect.senegal.zhiUtils.StringUtil;

public class ZHIRangeMobileInput extends RelativeLayout {

    private final Context mContext;

    private final String mMainText;

    private TextView et_input;

    private ImageView iv_compare_del;

    public ZHIRangeMobileInput(Context context) {
        this(context, null);
    }

    public ZHIRangeMobileInput(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIRangeMobileInput(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ZHIRangeMobileInput);
        mMainText = ta.getString(R.styleable.ZHIRangeMobileInput_mainText);
        ta.recycle();

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        View mView = View.inflate(mContext, R.layout.view_range_mobile_input, this);

        TextView tv_title = mView.findViewById(R.id.tv_title);
        tv_title.setText(mMainText);

        iv_compare_del = mView.findViewById(R.id.iv_compare_del);

        et_input = mView.findViewById(R.id.et_input);
        et_input.setOnClickListener(v -> showPickerDialog());

        iv_compare_del.setOnClickListener(v -> {
            String hintText = mContext.getResources().getString(R.string.hint_user_mobile);
            String startDate = StringUtil.safeString(et_input.getText().toString().trim());
            if (startDate.equalsIgnoreCase(hintText)) {
                showPickerDialog();
            } else {
                updateMobile(true, null);
            }
        });
    }

    private void showPickerDialog() {
        String title = mContext.getResources().getString(R.string.user_mobile);
        String content = getMobile();
        String hintText = mContext.getResources().getString(R.string.hint_user_mobile);
        ZHIDialogNumberInput.showDialog(mContext, title, content, hintText, new ZHIDialogItemClickListener() {
            @Override
            public void onConfirmClick(String content) {
                updateMobile(false, StringUtil.safeString(content));
            }

            @Override
            public void onCancelClick() {

            }
        });
    }

    private void updateMobile(boolean reset, String data) {
        try {
            if (reset) {
                et_input.setText(mContext.getResources().getString(R.string.hint_user_mobile));
                et_input.setTextColor(mContext.getResources().getColor(R.color.theme_transparent_7));
                iv_compare_del.setImageResource(R.drawable.ic_125);
                return;
            }

            if (TextUtils.isEmpty(data)) {
                return;
            }

            et_input.setText(data);
            et_input.setTextColor(mContext.getResources().getColor(R.color.theme_color_0));
            iv_compare_del.setImageResource(R.drawable.ic_124);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void reset() {
        updateMobile(true, null);
    }

    public String getMobile() {
        String hintText = mContext.getResources().getString(R.string.hint_user_mobile);
        String tempValue = StringUtil.safeString(et_input.getText().toString().trim());
        if (tempValue.equalsIgnoreCase(hintText)) {
            tempValue = "";
        }
        return tempValue;
    }

}
